/*
 * Copyright 2015 MingxingWang

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.audaque.graphdb.example

import java.io.File

import com.audaque.graphdb.{LGraph, LGraphDB}

/**
 * Created by Ming on 2015/4/21.
 */
object ClientTest {
  var graphdb: LGraphDB = new LGraphDB(new File("GraphDB"))
  var graph: LGraph = null

  def openGraph(graphName: String) {
    try {
      graph = graphdb.openGraph(graphName)
    } catch {
      case e: Throwable => println(e)
    }
  }

  def findEdge(fs: Array[String]) {
    try {
      val srcId = fs(1).toLong
      val dstId = fs(2).toLong
      graph.findEdge(srcId, dstId) match {
        case Some(edge) => edge.debug()
        case None => println("None.")
      }
    } catch {
      case _: Throwable =>
    }
  }

  def findVertex(fs: Array[String]) {
    try {
      for (i <- 1 until fs.length) {
        graph.findVertex(fs(i).toLong) match {
          case None => println("None.")
          case Some(v) => v.debug()
        }
      }
    } catch {
      case _: Throwable =>
    }
  }

  def searchVertex(fs: Array[String]) {
    try {
      val value = fs(1)
      val fuzzy =
        if (fs.length > 2) {
          fs(2).toBoolean
        } else {
          false
        }
      val num =
        if (fs.length > 3) {
          fs(3).toInt
        } else {
          10
        }
      graph.searchVertex(value, fuzzy, num).foreach(v => v.debug())
    } catch {
      case _: Throwable =>
    }
  }

  def findOutEdge(fs: Array[String]) {
    try {
      val vid = fs(1).toLong
      graph.findOutEdges(vid).foreach(e => e.debug())
    } catch {
      case _: Throwable =>
    }
  }

  def findInEdge(fs: Array[String]) {
    try {
      val vid = fs(1).toLong
      graph.findInEdges(vid).foreach(e => e.debug())
    } catch {
      case _: Throwable =>
    }
  }

  def findOutVertex(fs: Array[String]) {
    try {
      val vid = fs(1).toLong
      graph.findOutVertices(vid).foreach(v => v.debug())
    } catch {
      case _: Throwable =>
    }
  }

  def findInVertex(fs: Array[String]) {
    try {
      val vid = fs(1).toLong
      graph.findInVertices(vid).foreach(v => v.debug())
    } catch {
      case _: Throwable =>
    }
  }

  def main(args: Array[String]) {
    println("Please input(q for quit):")
    while (true) {
      try {
        val line = readLine()
        if ("q".equals(line) || "quit".equals(line)) {
          return
        }
        val fs = line.split("[\\s,]+")
        if (fs.length > 1) {
          fs(0).toLowerCase match {
            case "opengraphdb" => graphdb = new LGraphDB(new File(fs(1)))
            case "opengdb" => graphdb = new LGraphDB(new File(fs(1)))
            case "opengraph" => openGraph(fs(1))
            case "open" => openGraph(fs(1))
            case "findvertex" => findVertex(fs)
            case "fv" => findVertex(fs)
            case "searchvertex" => searchVertex(fs)
            case "sv" => searchVertex(fs)
            case "findoutedge" => findOutEdge(fs)
            case "foe" => findOutEdge(fs)
            case "findinedge" => findInEdge(fs)
            case "fie" => findInEdge(fs)
            case "findoutvertex" => findOutVertex(fs)
            case "fov" => findOutVertex(fs)
            case "findinvertex" => findInVertex(fs)
            case "fiv" => findInVertex(fs)
            case _: String => println("Unknown command.")
          }
          println("Down.")
        }
      } catch {
        case e: Throwable => println(e)
      }
    }

  }
}
